<?php

namespace app\web\controller;

use app\common\controller\Frontend;
use app\common\model\customer\Customer as customerModel;
use app\common\model\data\Voucher;
use app\common\model\goods\GoodsHouseStock;
use app\common\model\sales\Change as changemodel;
use app\common\model\sales\Checkout;
use app\common\model\sales\Slip;
use think\Db;
use think\Exception;

class Change extends Frontend
{

    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    protected $layout = '';


    public function index(){
        $model = new changemodel();
        $status = $model->change_status();
//        $where['storehouse_id'] = session('admin')['storehouse_id'];
        $storehouse_id = empty(session('admin')['storehouse_id'])?1:session('admin')['storehouse_id'];
        $where['storehouse_id'] = $storehouse_id;
        $list = Db::name('sales_change')
            ->field('s.id,s.number,explain,s.operator,s.company,actual_cost,h.name,c.company as ccompany,s.status')
            ->alias('s')
            ->join('storehouse h','s.storehouse_id = h.id')
            ->join('customer c','s.customer_id = c.id')
            ->where($where)
            ->limit(0,10)
            ->order('s.id','DESC')
            ->select();
        $total = Db::name('sales_change')
            ->alias('s')
            ->join('storehouse h','s.storehouse_id = h.id')
            ->join('customer c','s.customer_id = c.id')
            ->where($where)
            ->count();
        foreach ($list as &$v){
            $v['status_txt'] = $status[$v['status']];
        }
        $this->assign('total',$total);
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    public function getchange(){

        $page = input('page/d',1);
        $status = input('status/d',0);
        $pagesize = input('pagesize/d',10);
        $where = [];
//        $where['storehouse_id'] = session('admin')['storehouse_id'];
        $storehouse_id = empty(session('admin')['storehouse_id'])?1:session('admin')['storehouse_id'];
        $where['storehouse_id'] = $storehouse_id;
        if($status){
           $where['s.status'] = $status;
        }

        $model = new changemodel;
        $status = $model->change_status();
        $list = Db::name('sales_change')
            ->field('s.id,s.number,explain,s.operator,s.company,actual_cost,h.name,c.company as ccompany,s.status')
            ->alias('s')
            ->join('storehouse h','s.storehouse_id = h.id')
            ->join('customer c','s.customer_id = c.id')
            ->limit($page*$pagesize,$pagesize)
            ->where($where)
            ->order('s.id','DESC')
            ->select();
//        $total = Db::name('sales_slip')
//            ->alias('s')
//            ->join('storehouse h','s.storehouse_id = h.id')
//            ->join('customer c','s.customer_id = c.id')
//            ->where($where)
//            ->count();
        foreach ($list as &$v){
            $v['status_txt'] = $status[$v['status']];
        }
        $this->success('','',$list);
    }

    public function detail(){
        $ids =input('ids/d',0);
        $model = new changemodel();
        $row = $model->find($ids);
        if(empty($ids) || empty($row)){
            $this->error('查无数据');
        }

        $list = Db::name('sales_change_goods')
            ->where(array('c.sales_change_id'=>$ids))
            ->alias('c')
            ->join('goods g','c.goods_id = g.id','LEFT')
            ->join('supplier_brands b','g.brands_id = b.id','LEFT')
            ->select();

        $Checkout = new Checkout;
        $crow = $Checkout->find($row['sales_checkout_id']);

        $crow = \app\common\model\customer\Customer::find($crow['customer_id']);
        $contacts = empty($row['contacts'])?$crow['contacts']:$row['contacts'];
        $mobile = empty($row['mobile'])?$crow['mobile']:$row['mobile'];
        $address = empty($row['address'])?$crow['address']:$row['address'];


        $this->assign('ids',$ids);
        $this->assign('list',$list);
        $this->assign('row',$row);
        $this->assign('contacts',$contacts);
        $this->assign('mobile',$mobile);
        $this->assign('address',$address);
        return $this->view->fetch();
    }

}
